package comp5511.project04;

import java.io.*;

public class MyFileWriter {
	
	private FileWriter txtFileWriter;
	
	private BufferedWriter strWriterBuffer;

	public MyFileWriter(){
		File txtFile = new File("output.txt");
		try{
			if (txtFile.exists()) {
				System.out.println("output file exist! The old content will be removed");
				txtFileWriter = new FileWriter(txtFile, false);
				strWriterBuffer = new BufferedWriter(txtFileWriter);
			} else {
				if (txtFile.createNewFile()) {
					System.out.println("File successfully created");
					txtFileWriter = new FileWriter(txtFile, false);
					strWriterBuffer = new BufferedWriter(txtFileWriter);
				} else {
					System.out.println("File created Failed");
					return;
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Write content to the file
	 * 
	 * @param str
	 */
	public void wirteTXT(String str) {
		try {
			strWriterBuffer.append(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("FileWriter ERROR");
			e.printStackTrace();
		}
	}

	public void closeMyFile() throws IOException {
		strWriterBuffer.close();
	}

}
